package com.sgcc.sx.pmos.common.utils;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.PhoneUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupPosition;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.lxj.xpopup.util.SmartGlideImageLoader;
import com.lxj.xpopup.util.XPopupUtils;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.sx.pmos.common.bean.BaseResponse;
import com.sgcc.sx.pmos.common.bean.StoreScreenBackBean;
import com.sgcc.sx.pmos.common.dialog.EnterpriseDetailView;
import com.sgcc.sx.pmos.common.dialog.FirstTipsDialog;
import com.sgcc.sx.pmos.common.dialog.GoodScreenDialog;
import com.sgcc.sx.pmos.common.dialog.LineChartDialog;
import com.sgcc.sx.pmos.common.dialog.OnSearchClickListener;
import com.sgcc.sx.pmos.common.dialog.StoreScreenDialog;
import com.sgcc.sx.pmos.common.http.HttpInterConst;
import com.sgcc.sx.pmos.common.http.HttpRequestUtils;
import com.sgcc.sx.pmos.common.http.MvcNetCallback;
import com.sgcc.sx.pmos.common.http.MvpNetCallback;
import com.sgcc.sx.pmos.common.widget.TagOtherView;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * @author ChaoAJ
 * @describe 弹窗弹出Util
 * @time 2022/8/29 14:11
 */
public class DialogUtil {

    /**
     * 企业详细信息弹窗
     *
     * @param context 上下文
     */
    public static void showEnterpriseDetailDialog(Context context) {
        new XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .isViewMode(true)
                .asCustom(new EnterpriseDetailView(context))
                .show();
    }

    /**
     * 曲线图弹窗
     *
     * @param context 上下文
     */
    public static void showLineChartDialog(Context context, List<String> list) {
        new XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .isViewMode(true)
                .asCustom(new LineChartDialog(context, list))
                .show();
    }

    /**
     * 曲线图弹窗 两条曲线
     *
     * @param context 上下文
     */
    public static void showLineChartTwoLineDialog(Context context,
                                                  List<String> list1, List<String> list2) {
        new XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .isViewMode(true)
                .asCustom(new LineChartDialog(context, list1, list2))
                .show();
    }


    /**
     * @param context
     * @param title
     * @param content
     * @param onConfirmListener 确认回调
     */
    public static void showConfirm(Context context, String title, String content, OnConfirmListener onConfirmListener) {
        new XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .asConfirm(title, content,
                        "取消", "确定",
                        onConfirmListener, null, false).show();
    }

 public static void showConfirm(Context context, String title, String content,boolean isHideCancel, OnConfirmListener onConfirmListener) {
        new XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .asConfirm(title, content,
                        "取消", "确定",
                        onConfirmListener, null, isHideCancel).show();
    }


    /**
     * 拨打电话弹窗
     *
     * @param context 上下文
     * @param phone   电话号
     */
    public static void showCallPhone(Context context, String phone) {

        showConfirm(context, "提示", "确认给 " + phone + " 拨打电话？", new OnConfirmListener() {
            @Override
            public void onConfirm() {
                PhoneUtils.dial(phone);
            }
        });


    }

    /**
     * 店铺筛选弹窗
     *
     * @param context 上下文
     */
    public static void showStoreScreenDialog(Context context, OnSearchClickListener<StoreScreenBackBean> onSearchClickListener) {
        new XPopup.Builder(context)
                .popupPosition(PopupPosition.Right)//右边
                .hasStatusBarShadow(true) //启用状态栏阴影
                .asCustom(new StoreScreenDialog(context, onSearchClickListener)).show();

    }

    /**
     * 查看一张图片
     *
     * @param context
     * @param imageView
     * @param url
     */
    public static void showImageDialog(Context context, ImageView imageView, Object url) {
        if (null == url)
            return;
        new XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .hasNavigationBar(false)
                .asImageViewer(imageView, url, true, Color.parseColor("#f1f1f1"), -1, 0
                        , false, Color.BLACK, new SmartGlideImageLoader(R.mipmap.ic_launcher), null)
                .show();

    }

    /**
     * 第一次打开app 温馨提示
     *
     * @param context
     */
    public static void showFirstTipsDialog(Context context) {
        new XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(new FirstTipsDialog(context))
                .show();
    }


    private static TagOtherView inputCodeDialog;

    /**
     * 输入验证码弹窗
     *
     * @param context
     * @param flag
     * @param onInputCodeVerifyCodeListener
     */
    public static void showInputCodeDialog(Context context, int flag,
                                           OnInputCodeVerifyCodeListener onInputCodeVerifyCodeListener) {
        sendCode(flag);
        inputCodeDialog = (TagOtherView) new XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .autoOpenSoftInput(true)
                .asCustom(new TagOtherView(context, new TagOtherView.OnListener() {
                    @Override
                    public void onPositiveClick(String code) {
                        if (TextUtils.isEmpty(code)) {
                            ToastUtils.showShort("请填写短信验证码");
                        } else {
                            verifyCode(code, onInputCodeVerifyCodeListener);
                        }

                    }

                    @Override
                    public void onCompleteClick(String code) {

                    }

                    @Override
                    public void onReSendClick() {
                        sendCode(flag);
                    }
                })).show();

    }

    public interface OnInputCodeVerifyCodeListener {

        void onVerifyCode();

    }


    private static void verifyCode(String code, OnInputCodeVerifyCodeListener onInputCodeVerifyCodeListener) {

        Map<String, Object> params = new HashMap<>();
        params.put("_t", System.currentTimeMillis());
        params.put("code", code);
        HttpRequestUtils.getInstance().request(params, HttpInterConst.ORDER_VERIFYCODE,
                new MvcNetCallback<BaseResponse>() {
                    @Override
                    public void onResponse(BaseResponse response, int id) {
                        super.onResponse(response, id);
                        if (response.getCode().equals("200")) {
                            if (null != inputCodeDialog) {
                                inputCodeDialog.dismiss();
                            }
                            onInputCodeVerifyCodeListener.onVerifyCode();
                        } else {
                            if (null != inputCodeDialog)
                                inputCodeDialog.clearCode();
                            ToastUtils.showShort(response.getMsg());
                        }
                    }
                });
    }

    private static void sendCode(int flag) {
        Map<String, Object> params = new HashMap<>();
        params.put("flag", flag);
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.ORDER_SENDMESSAGE_NEW,
                new MvcNetCallback<BaseResponse>() {
                    @Override
                    public void onResponse(BaseResponse response, int id) {
                        super.onResponse(response, id);
                        ToastUtils.showShort(response.getMsg());
                    }
                });


    }

    /**
     * @param showType new boolean[]{true, true, true, false, false, false}
     *                 分别控制“年”“月”“日”“时”“分”“秒”的显示或隐藏。
     */
    public static void showDateSelectDialog(Context mContext, boolean[] showType,
                                            Calendar startDate,Calendar endDate,  Calendar selectedDate,
                                            OnTimeSelectListener onTimeSelectListener) {



        TimePickerView pvTime = new TimePickerBuilder(mContext, onTimeSelectListener)
                .setType(showType)// 默认全部显示
                .setContentTextSize(16)//设置滚轮文字大小
                .setLineSpacingMultiplier(1.8f)
                .setDividerColor(Color.parseColor("#DCDCDC"))//设置分割线的颜色
                .setBgColor(Color.WHITE)
                .setTitleBgColor(Color.WHITE)
                .setRangDate(startDate, endDate)
                .setDate(selectedDate)
                .setTextColorOut(ColorUtils.getColor(R.color._999999))
                .setCancelColor(Color.parseColor("#333333"))
                .setSubmitColor(Color.parseColor("#0092FF"))
                .setTextColorCenter(Color.parseColor("#333333"))
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .build();
        pvTime.show();


    }

}
